package com.birboot.core;

import com.birboot.http.HttpServletRequest;
import com.birboot.http.HttpServletResponse;
import org.w3c.dom.ls.LSOutput;

import javax.sound.sampled.Line;
import javax.swing.plaf.PanelUI;
import java.io.*;
import java.net.Socket;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.SplittableRandom;

public class ClientHandler  implements Runnable {
    private Socket socket;

    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {

            //1解析请求
            HttpServletRequest request = new HttpServletRequest(socket);
            HttpServletResponse response = new HttpServletResponse(socket);

            //2 处理请求
            DispatcherServlet servlet = new DispatcherServlet();
            servlet.service(request, response);

            //3 发送响应
            response.response();

        } catch (IOException | URISyntaxException e){
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


